<?php

namespace app\stores\model\order;


use app\common\model\CommonModel;


use app\beautiful\model\order\GoodsOrderItemModel;
use app\common\model\ShopModel;
use think\facade\Db;

class GoodsOrderModel extends CommonModel{

    protected  $name = 'beautiful_order';

    protected  $pk   = 'order_id';

    protected $type=[

        'pay_info' => 'array',

    ];


    public function getAddTimeAttr($value,$data){

        return date('Y-m-d H:i:s',$data['add_time']);

    }
    public function getPayTimeAttr($value,$data){

        return date('Y-m-d H:i:s',$data['pay_time']);

    }
    public function skus(){

        return  $this->hasMany(GoodsOrderItemModel::class,'order_id','order_id');

    }

    public function shop(){

        return  $this->hasOne(ShopModel::class,'shop_id','shop_id');

    }
    public static function list($where,$order,$page=1,$limit=10){
        $query = Db::name('beautiful_order')->alias('a')
            ->where(['a.is_delete'=>0,'a.from_type'=>2])
//            ->whereIn('a.status',[4,8,9,10])
            ->when(!empty($where['pay_time']),function ($query) use ($where){
                $query->where('a.pay_time','>',$where['pay_time']);
            })
            ->when(!empty($where['>=pay_time']),function ($query) use ($where){
                $query->where('a.pay_time','>=',$where['pay_time']);
            })
            ->when(!empty($where['edit_time']),function ($query) use ($where){
                $query->where('a.edit_time','>',date('Y-m-d H:i:s',$where['edit_time']));
            })
            ->when(!empty($where['>=edit_time']),function ($query) use ($where){
                $query->where('a.edit_time','>=',date('Y-m-d H:i:s',$where['>=edit_time']));
            })
            ->when(!empty($where['<=edit_time']),function ($query) use ($where){
                $query->where('a.edit_time','<=',date('Y-m-d H:i:s',$where['<=edit_time']));
            })
            ->when(!empty($where['status']),function ($query) use ($where){
                $query->where('a.status','=',$where['status']);
            })
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where('a.shop_id','=',$where['shop_id']);
            })
            ->when(!empty($where['is_paid']),function ($query) use ($where){
                $query->where('a.is_paid','=',$where['is_paid']);
            })
            ->when(isset($where['is_settle']),function ($query) use ($where){
                $query->where('a.is_settle','=',$where['is_settle']);
            })
            ->when(!empty($where['not_type']),function ($query) use ($where){
                $query->whereNotIn('a.order_type',$where['not_type']);
            })
            ->field("a.*");
        $query=$query->order($order);
        if(empty($where['is_all'])){
            $query=$query->limit($limit)->page($page);
        }
        $list=$query->select()->toArray();
        return $list;
    }
    public static function user_order($where){
        $query = Db::name('beautiful_order')->alias('a')
            ->join('beautiful_member b','a.member_id=b.member_id')
            ->where(['a.is_delete'=>0,'a.from_type'=>2])
            ->whereIn('a.status',[2,3,4,8,9,10])
            ->when(!empty($where['order_id']),function ($query) use ($where){
                $query->where(['a.order_id'=>$where['order_id']]);
            })
            ->when(!empty($where['member_id']),function ($query) use ($where){
                $query->where(['a.member_id'=>$where['member_id']]);
            })
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(!empty($where['status']),function ($query) use ($where){
                $query->where(['a.status'=>$where['status']]);
            })
            ->when(!empty($where['pid']),function ($query) use ($where){
                $query->where(['b.pid'=>$where['pid']]);
            })
            ->when(isset($where['is_paid']),function ($query) use ($where){
                $query->where(['a.is_paid'=>$where['is_paid']]);
            })
            ->when(!empty($where['start_time']),function ($query) use ($where){
                $query->where('a.edit_time','>=',date('Y-m-d H:i:s',$where['start_time']));
            })
            ->when(!empty($where['end_time']),function ($query) use ($where){
                $query->where('a.edit_time','<=',date('Y-m-d H:i:s',$where['end_time']));
            })
            ->field("sum(a.need_pay) as money,count(a.order_id) as all_num,sum(a.all_goods_gross) as all_goods_gross,a.member_id,b.nick_name");
        $info=$query->find();
//        var_dump(Db::getlastSQL());exit();
        return $info;
    }
    public static function max_money($where){
        $query = Db::name('beautiful_order')->alias('a')
            ->join('beautiful_member b','a.member_id=b.member_id')
            ->where(['a.is_delete'=>0,'a.from_type'=>2])
            ->whereIn('a.status',[4,8,9,10])
            ->when(!empty($where['member_id']),function ($query) use ($where){
                $query->where(['a.member_id'=>$where['member_id']]);
            })
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(!empty($where['status']),function ($query) use ($where){
                $query->where(['a.status'=>$where['status']]);
            })
            ->when(!empty($where['pid']),function ($query) use ($where){
                $query->where(['b.pid'=>$where['pid']]);
            })
            ->when(!empty($where['start_time']),function ($query) use ($where){
                $query->where('a.edit_time','>=',date('Y-m-d H:i:s',$where['start_time']));
            })
            ->when(!empty($where['end_time']),function ($query) use ($where){
                $query->where('a.edit_time','<=',date('Y-m-d H:i:s',$where['end_time']));
            })
            ->field("a.need_pay as money,a.member_id")
            ->order(['a.need_pay'=>'desc']);
        $info=$query->find();
        $money=0;
        if(!empty($info)){
            $money=$info['money'];
        }
        return $money;
    }
    public static function getCount($where){
        $query = Db::name('beautiful_order_item')->alias('oi')
            ->join('beautiful_order o','o.order_id=oi.order_id')
            ->where(['o.from_type'=>2,'o.is_delete'=>0])
            ->when(!empty($where['order_type']),function ($query) use ($where){
                $query->where(['o.order_type'=>$where['order_type']]);
            })
            ->when(!empty($where['member_id']),function ($query) use ($where){
                $query->where(['o.member_id'=>$where['member_id']]);
            })
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['o.shop_id'=>$where['shop_id']]);
            })
            ->when(!empty($where['start_time']),function ($query) use ($where){
                $query->where('o.add_time >= '.$where['start_time']);
            })
            ->when(!empty($where['end_time']),function ($query) use ($where){
                $query->where('o.add_time <= '.$where['end_time']);
            })
            ->when(!empty($where['not_status']),function ($query) use ($where){
                $query->whereNotIn('o.status',$where['not_status']);
            })
            ->when(!empty($where['goods_id']),function ($query) use ($where){
                $query->where(['oi.goods_id'=>$where['goods_id']]);
            });
        $count=$query->sum('oi.goods_num');

        return compact('count');
    }
}



